Coverage Report

Created: 2024-12-26 12:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\api\tools\mod.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
mod error;
30
31
use crate::api::config;
32
use crate::api::core::generator::{Context, Generator};
33
use crate::compiler::util::imports::ImportSolver;
34
use crate::gen::template::loader::TemplateLoader;
35
use crate::gen::Generator as Gen;
36
use bp3d_debug::debug;
37
pub use error::Error;
38
use serde::Deserialize;
39
use std::path::Path;
40
41
pub trait GenTools {
42
    type Params<'a>: Deserialize<'a>;
43
    type Generator: crate::gen::Generator;
44
    type Solver: ImportSolver;
45
46
    fn new_solver() -> Self::Solver;
47
    fn new_generator() -> Self::Generator;
48
    fn generate<'b>(
49
        generator: &'b Generator<'_, Self::Generator>,
50
        context: &mut Context<'b, Self::Solver>,
51
        config: &config::model::Config<Self::Params<'_>>,
52
    ) -> Result<(), Error>;
53
54
3
    fn run(
55
3
        config: &config::model::Config<Self::Params<'_>>,
56
3
        out_dir: impl AsRef<Path>,
57
3
        post_generation: impl FnOnce(&Context<Self::Solver>),
58
3
    ) -> Result<(), Error> {
59
3
        let motherfuckingrust = Self::new_solver();
60
3
        let protocols = config::core::compile(config, &motherfuckingrust)
?0
;
61
3
        let mut loader = TemplateLoader::new();
62
3
        let path = Path::new("./codec/");
63
3
        debug!(
64
3
            "Adding codec path {}...",
65
3
            path.canonicalize().unwrap_or_default().display()
66
3
        );
67
3
        debug!(
68
3
            "Current folder: {}",
69
3
            Path::new(".").canonicalize().unwrap_or_default().display()
70
3
        );
71
3
        loader.add_search_path(path);
72
3
        let mut codecs = Self::Generator::get_default_codecs();
73
39
        for v in 
protocols.iter().flat_map(3
|v|
v.iter_codecs()38
)3
{
74
39
            if !codecs.has(v) {
  Branch (74:16): [True: 4, False: 18]
  Branch (74:16): [Folded - Ignored]
  Branch (74:16): [True: 4, False: 13]
  Branch (74:16): [Folded - Ignored]
75
8
                loader.load(String::from(v)).map_err(Error::TemplateLoader)
?0
;
76
31
            }
77
        }
78
39
        for v in 
protocols.iter().flat_map(3
|v|
v.iter_codecs()38
)3
{
79
39
            if !codecs.has(v) {
  Branch (79:16): [True: 2, False: 20]
  Branch (79:16): [Folded - Ignored]
  Branch (79:16): [True: 2, False: 15]
  Branch (79:16): [Folded - Ignored]
80
4
                let template = loader.compile(v, &codecs).map_err(Error::TemplateLoader)
?0
;
81
4
                codecs.insert(v, template);
82
35
            }
83
        }
84
3
        let mut generator = Generator::new(out_dir.as_ref(), codecs, Self::new_generator());
85
3
        if let Some(
file_header0
) = config.package.file_header {
  Branch (85:16): [True: 0, False: 2]
  Branch (85:16): [Folded - Ignored]
  Branch (85:16): [True: 0, False: 1]
  Branch (85:16): [Folded - Ignored]
86
0
            generator.set_file_header(file_header);
87
3
        }
88
3
        let mut context = Context::new(&protocols);
89
3
        Self::generate(&generator, &mut context, config)
?0
;
90
3
        post_generation(&context);
91
3
        Ok(())
92
3
    }
93
94
3
    fn run_string(
95
3
        config: impl AsRef<str>,
96
3
        out_dir: impl AsRef<Path>,
97
3
        post_generation: impl FnOnce(&Context<Self::Solver>),
98
3
    ) -> Result<(), Error> {
99
3
        let config = config::core::parse::<Self::Params<'_>>(config.as_ref()).map_err(Error::Config)
?0
;
100
3
        Self::run(&config, out_dir, post_generation)
101
3
    }
102
103
3
    fn run_file(
104
3
        config_file: impl AsRef<Path>,
105
3
        out_dir: impl AsRef<Path>,
106
3
        post_generation: impl FnOnce(&Context<Self::Solver>),
107
3
    ) -> Result<(), Error> {
108
3
        let str = std::fs::read_to_string(config_file).map_err(Error::Io)
?0
;
109
3
        Self::run_string(str, out_dir, post_generation)
110
3
    }
111
}
112
113
#[cfg(feature = "gen-rust")]
114
mod rust;
115
#[cfg(feature = "gen-swift")]
116
mod swift;
117
118
#[cfg(feature = "gen-rust")]
119
pub use rust::Rust;
120
121
#[cfg(feature = "gen-swift")]
122
pub use swift::Swift;